package queue_stack.leetcode.medium;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author bruin_du
 * @description 后缀表达式
 * @date 2022/8/12 23:16
 **/
public class OfferII036_EvalRPN {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new ArrayDeque<>();
        for (String str : tokens) {
            if (str.equals("-") || str.equals("+") || str.equals("/") || str.equals("*")) {
                int x1 = stack.pop();
                int x2 = stack.pop();
                int res = 0;
                if (str.equals("-")) res = x2 - x1;
                else if (str.equals("+")) res = x2 + x1;
                else if (str.equals("/")) res = x2 / x1;
                else if (str.equals("*")) res = x2 * x1;
                stack.push(res);
            } else {
                stack.push(Integer.parseInt(str));
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        OfferII036_EvalRPN offer = new OfferII036_EvalRPN();
        offer.evalRPN(new String[]{"2","1","+","3","*"});
    }
}
